package com.yssw.customer.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.yssw.common.core.domain.entity.SysDictData;
import com.yssw.common.utils.DateUtils;
import com.yssw.system.mapper.SysDictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yssw.customer.domain.ServiceType;
import com.yssw.customer.service.IServiceTypeService;

/**
 * 服务类型Service业务层处理
 *
 * @author yssw
 * @date 2023-03-28
 */
@Service
public class ServiceTypeServiceImpl implements IServiceTypeService {
    @Autowired
    private SysDictDataMapper dictDataMapper;

    /**
     * 查询服务类型
     *
     * @param id 服务类型主键
     * @return 服务类型
     */
    @Override
    public ServiceType selectServiceTypeById(Long id) {
        SysDictData sysDictData = dictDataMapper.selectDictDataById(id);
        ServiceType serviceType = new ServiceType();
        serviceType.setTypeName(sysDictData.getDictLabel());
        serviceType.setRemark(sysDictData.getRemark());
        serviceType.setId(sysDictData.getDictCode());
        return serviceType;
    }

    /**
     * 查询服务类型列表
     *
     * @param serviceType 服务类型
     * @return 服务类型
     */
    @Override
    public List<ServiceType> selectServiceTypeList(ServiceType serviceType) {
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictLabel(serviceType.getTypeName());
        sysDictData.setDictCode(serviceType.getId());
        sysDictData.setDictType("service_type");
        List<ServiceType> serviceTypes=new ArrayList<>();
        List<SysDictData> sysDictDataList = dictDataMapper.selectDictDataList(sysDictData);
        sysDictDataList.forEach(sysDictData1 -> {
            ServiceType serviceType1=new ServiceType();
            serviceType1.setId(sysDictData1.getDictCode());
            serviceType1.setTypeName(sysDictData1.getDictLabel());
            serviceType1.setRemark(sysDictData1.getRemark());
            serviceTypes.add(serviceType1);
        });
        return serviceTypes;
    }

    /**
     * 新增服务类型
     *
     * @param serviceType 服务类型
     * @return 结果
     */
    @Override
    public int insertServiceType(ServiceType serviceType) {
        SysDictData sysDictData = new SysDictData();
        int max = dictDataMapper.selectDictDataValMaxByDictType("service_type");
        String val=(max+1)+"";
        sysDictData.setDictValue(val);
        sysDictData.setRemark(serviceType.getRemark());
        sysDictData.setCreateTime(DateUtils.getNowDate());
        sysDictData.setDictLabel(serviceType.getTypeName());
        sysDictData.setDictType("service_type");
        return dictDataMapper.insertDictData(sysDictData);
    }

    /**
     * 修改服务类型
     *
     * @param serviceType 服务类型
     * @return 结果
     */
    @Override
    public int updateServiceType(ServiceType serviceType) {
        SysDictData sysDictData = new SysDictData();
        sysDictData.setUpdateTime(DateUtils.getNowDate());
        sysDictData.setDictCode(serviceType.getId());
        sysDictData.setDictLabel(serviceType.getTypeName());
        sysDictData.setDictType("service_type");
        sysDictData.setRemark(serviceType.getRemark());
        return dictDataMapper.updateDictData(sysDictData);
    }

    /**
     * 批量删除服务类型
     *
     * @param ids 需要删除的服务类型主键
     * @return 结果
     */
    @Override
    public int deleteServiceTypeByIds(Long[] ids) {
        return dictDataMapper.deleteDictDataByIds(ids);
    }

    /**
     * 删除服务类型信息
     *
     * @param id 服务类型主键
     * @return 结果
     */
    @Override
    public int deleteServiceTypeById(Long id) {
        return dictDataMapper
                .deleteDictDataById(id);
    }
}
